// <copyright file="InterpolationIER.cs" company="MetricGR">
// Copyright MetricGR. All rights reserved.
// </copyright>
// <author>MAGNA\Andtheflux</author>
// <date>2015-07-24 13:38:48Z</date>
namespace MetricGR.Interpolation.Components
{
    using System;
    using System.Collections.Generic;
    using System.Security.Permissions;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;

    /// <summary>
    /// TODO: Add comment for InterpolationIER
    /// </summary>
    public class InterpolationIER : SPItemEventReceiver
    {
        /// <summary>
        /// TODO: Add comment for event ItemUpdated in InterpolationIER 
        /// </summary>
        /// <param name="properties">Contains list event properties</param>   
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            EventFiringEnabled = false;

            ////Add code for event ItemUpdated in InterpolationIER 
            SPSite site = properties.List.ParentWeb.Site;
            SPWeb web = properties.List.ParentWeb;
            SPList list = properties.List;
            SPListItem listitem = properties.ListItem;

            foreach (SPField field in properties.List.Fields)
            {
                string interpolationstring = field.GetProperty(Common.FieldInterpolationProperty);
                if (!string.IsNullOrEmpty(interpolationstring))
                {
                    string value = InterpolationFieldControl.GetItemFieldInterpolatedString(site, web, list, listitem, field);
                    properties.ListItem[field.Id] = value;
                    properties.ListItem.SystemUpdate(false);
                }
            }

            EventFiringEnabled = true;
        }
    }
}

